﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Orders
{
	public partial class eTradingStastistic : WeifenLuo.WinFormsUI.Docking.DockContent  
	{
		public eTradingStastistic()
		{
			InitializeComponent();
		}
		
		private void load_grid()
		{
            DateTime trans_date = DeclareLibrary.MARKET_DAY;
            string board_id = string.Empty;
            string account_no = string.Empty;
            string stock_code = string.Empty;
           
            if (valueRow1.Cells["ACCOUNT_NO"].Value != null)
                account_no = valueRow1.Cells["ACCOUNT_NO"].Value.ToString();
            if (valueRow1.Cells["CODE"].Value != null)
                stock_code = valueRow1.Cells["CODE"].Value.ToString();
            if (valueRow1.Cells["TRANS_DATE"].Value != null)
                trans_date = Convert.ToDateTime(valueRow1.Cells["TRANS_DATE"].Value);
            if (valueRow1.Cells["BOARD_ID"].Value != null)
                board_id = valueRow1.Cells["BOARD_ID"].Value.ToString();
            
            DataSet v_data = ABSCommonLibrary.DoInquiry(
                CommonLibrary.CommonLibrary.BOSC013001, new object[4]{
                    trans_date, board_id, account_no, stock_code});
            gridControl1.SetDataBinding(v_data, v_data.Tables[0].TableName);

		}
			
		private void eTradingStastistic_Load(object sender, EventArgs e)
		{
			try
			{
                ABSCommonLibrary.CreateGridView(ref gridControl1);
                colTRANS_DATE.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;
                colBID_VOLUME.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
                colBID_PRICE.FormatSpecifier = DeclareLibrary.CURRENCY_FORMAT;
                colBID_VALUE.FormatSpecifier = DeclareLibrary.CURRENCY_FORMAT;
                colOFFER_VOLUME.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
                colOFFER_PRICE.FormatSpecifier = DeclareLibrary.CURRENCY_FORMAT;
                colOFFER_VALUE.FormatSpecifier = DeclareLibrary.CURRENCY_FORMAT;
                switch (this.Tag.ToString())
                {
                    case "00":
                        insertionRow1.Visible = false;
                        dataRowTemplate1.ReadOnly = true;
                        break;

                    case "02":
                        insertionRow1.Visible = true;
                        dataRowTemplate1.ReadOnly = false;
                        break;
                    default:
                        break;
                }
			}
			catch (Exception ex)
			{
				ABSCommonLibrary.ShowErrorMessage(ex.ToString());
			}
		}

		private void DeleteTradingTrans(DataRow row)
		{           
            object[] p_param = new object[1];
            p_param[0] = row["TTRANS_ID"];                       
            int err_code = Convert.ToInt16(ABSCommonLibrary.DoExecute(
                CommonLibrary.CommonLibrary.BOSC013013, ref p_param));

            if (err_code == 0)            
                ABSCommonLibrary.ShowMessage("Du lieu khop lenh cho xac nhan da dc xoa!");            
            else
                ABSCommonLibrary.ShowMessage("Du lieu khop lenh khong xoa duoc!");
		}

        private void UpdateTradingTrans(DataRow row)
        {
            object[] p_param = new object[4];
            p_param[0] = row["TTRANS_ID"];
            p_param[1] = row["ACCOUNT_NO"];
            p_param[2] = DeclareLibrary.MARKET_DAY;
            p_param[3] = DeclareLibrary.USER_NAME;
            int err_code = Convert.ToInt16(ABSCommonLibrary.DoExecute(
                CommonLibrary.CommonLibrary.BOSC013015, ref p_param));

            if (err_code == 0)
                ABSCommonLibrary.ShowMessage("Cap nhat deal khop lenh thanh cong");
            else
            {
                switch (err_code)
                {
                    case -1:
                        ABSCommonLibrary.ShowMessage("Phai dat lenh truoc khi cap nhat deal khop");
                        break;
                    case -2:
                        ABSCommonLibrary.ShowMessage("Tai khoan cap nhat khong ton tai");
                        break;
                    default:
                        ABSCommonLibrary.ShowMessage("Loi khong xac dinh duoc nguyen nhan");
                        break;
                }
                
            }
        }

		private void AddnewTradingTrans(DataRow row)
		{
            object[] p_param = new object[9];
            p_param[0] = row["TRANS_DATE"];
            p_param[1] = row["CONFIRM_NO"];
         
            p_param[2] = row["TRANS_TYPE"];            
            p_param[3] = row["CODE"];
            p_param[4] = row["ACCOUNT_NO"];
            if (row["TRANS_TYPE"].ToString() == "B")
            {
                p_param[5] = row["BID_VOLUME"];
                p_param[6] = row["BID_PRICE"];
            }
            else
            {
                p_param[5] = row["OFFER_VOLUME"];
                p_param[6] = row["OFFER_PRICE"];
            }
            
            p_param[7] = DeclareLibrary.MARKET_DAY;
            p_param[8] = DeclareLibrary.USER_NAME;
            int err_code =Convert.ToInt16( ABSCommonLibrary.DoExecute(
                CommonLibrary.CommonLibrary.BOSC013014, ref p_param));
            if (err_code == 0)
                ABSCommonLibrary.ShowMessage("Them moi deal khop lenh thanh cong");
            else
            {
                switch (err_code)
                {
                    case -1:
                        ABSCommonLibrary.ShowMessage("Phai cap nhat lenh dat truoc khi tao deal khop");
                        break;
                    case -2:
                        ABSCommonLibrary.ShowMessage("Tai khoan khach hang khong ton tai");
                        break;
                    case -3:
                        ABSCommonLibrary.ShowMessage("Chung khoan giao dich khong ton tai");
                        break;
                    case -4:
                        ABSCommonLibrary.ShowMessage("Trung so xac nhan cua deal khop");
                        break;
                    default:
                        ABSCommonLibrary.ShowMessage("Loi khong xac dinh duoc nguyen nhan");
                        break;
                }                
            }
		}

        private void eTradingStastistic_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F9)
                {
                    if (((DataSet)gridControl1.DataSource).Tables[0].GetChanges() != null)
                    {
                        //CenturyFox.Service data_access = new CenturyFox.Service();
                        foreach (DataRow row in ((DataSet)gridControl1.DataSource).Tables[0].GetChanges().Rows)
                        {
                            if (row.RowState == DataRowState.Added)
                            {
                                AddnewTradingTrans(row);
                            }
                            else if (row.RowState == DataRowState.Deleted)
                            {
                                DeleteTradingTrans(row);
                            }
                            else if (row.RowState == DataRowState.Modified)
                            {
                                UpdateTradingTrans(row);
                            }
                        }
                        load_grid();
                    }
                }            
                else if (e.KeyCode == Keys.F11)
                {
                    if (((DataSet)gridControl1.DataSource).Tables[0].GetChanges() != null)
                    {
                        if (ABSCommonLibrary.ShowQuestion(
                            "Bo qua nhung thay doi giao dich vua duoc tao?") == DialogResult.No)
                            return;
                    }
                    load_grid();
                }
                else if (e.KeyCode == Keys.F10)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                    saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
                    saveFileDialog.AddExtension = true;
                    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                    {
                        string FileName = saveFileDialog.FileName;
                        // TODO: Add code here to save the current contents of the form to a file.
                        Xceed.Grid.Exporting.ExcelExporter export = new Xceed.Grid.Exporting.ExcelExporter();
                        export.Export(gridControl1, FileName);
                    }
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
	}
}